// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network;

import gnu.trove.map.hash.TIntLongHashMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import zombie.core.Core;

public class ChunkChecksum {
    private static final TIntLongHashMap checksumCache = new TIntLongHashMap();
    private static final StringBuilder stringBuilder = new StringBuilder(128);
    private static final CRC32 crc32 = new CRC32();
    private static final byte[] bytes = new byte[1024];

    private static void noise(String var0) {
        if (Core.bDebug) {
        }
    }

    public static long getChecksum(int wx, int wy) throws IOException {
        MPStatistic.getInstance().ChunkChecksum.Start();
        long _long = 0L;
        synchronized (checksumCache) {
            int _int = wx + wy * 30 * 1000;
            if (checksumCache.containsKey(_int)) {
                noise(wx + "," + wy + " found in cache crc=" + checksumCache.get(_int));
                _long = checksumCache.get(_int);
            } else {
                stringBuilder.setLength(0);
                stringBuilder.append(zombie.ZomboidFileSystem.instance.getGameModeCacheDir());
                stringBuilder.append(File.separator);
                stringBuilder.append(Core.GameSaveWorld);
                stringBuilder.append(File.separator);
                stringBuilder.append("map_");
                stringBuilder.append(wx);
                stringBuilder.append("_");
                stringBuilder.append(wy);
                stringBuilder.append(".bin");
                _long = createChecksum(stringBuilder.toString());
                checksumCache.put(_int, _long);
                noise(wx + "," + wy + " read from disk crc=" + _long);
            }
        }

        MPStatistic.getInstance().ChunkChecksum.End();
        return _long;
    }

    public static long getChecksumIfExists(int wx, int wy) throws IOException {
        long _long = 0L;
        MPStatistic.getInstance().ChunkChecksum.Start();
        synchronized (checksumCache) {
            int _int = wx + wy * 30 * 1000;
            if (checksumCache.containsKey(_int)) {
                _long = checksumCache.get(_int);
            }
        }

        MPStatistic.getInstance().ChunkChecksum.End();
        return _long;
    }

    public static void setChecksum(int wx, int wy, long crc) {
        MPStatistic.getInstance().ChunkChecksum.Start();
        synchronized (checksumCache) {
            int _int = wx + wy * 30 * 1000;
            checksumCache.put(_int, crc);
            noise(wx + "," + wy + " set crc=" + crc);
        }

        MPStatistic.getInstance().ChunkChecksum.End();
    }

    public static long createChecksum(String filename) throws IOException {
        MPStatistic.getInstance().ChunkChecksum.Start();
        File file = new File(filename);
        if (!file.exists()) {
            MPStatistic.getInstance().ChunkChecksum.End();
            return 0L;
        } else {
            FileInputStream fileInputStream = new FileInputStream(filename);

            long long0;
            try {
                crc32.reset();

                int _int;
                while ((_int = fileInputStream.read(bytes)) != -1) {
                    crc32.update(bytes, 0, _int);
                }

                long long1 = crc32.getValue();
                MPStatistic.getInstance().ChunkChecksum.End();
                long0 = long1;
            } catch (Throwable throwable0) {
                try {
                    fileInputStream.close();
                } catch (Throwable throwable1) {
                    throwable0.addSuppressed(throwable1);
                }

                throw throwable0;
            }

            fileInputStream.close();
            return long0;
        }
    }

    public static void Reset() {
        MPStatistic.getInstance().ChunkChecksum.Start();
        checksumCache.clear();
        MPStatistic.getInstance().ChunkChecksum.End();
    }
}
